/**
 * Terminate_nodes.java
 * ************************** 
 * @date Apr 15, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package infrastructureAPI.scripts.instanceTools;

import java.net.UnknownHostException;
import java.util.List;
import java.util.Vector;

import gossipServices.basic.nodeDescriptors.ConcreteNodeDescriptor;
import gossipServices.basic.nodeDescriptors.NodeDescriptor;
import infrastructureAPI.ResponseException;
import infrastructureAPI.APIMessages.ResponseMessage;
import infrastructureAPI.algorithms.instanceTools.TerminateNodes;
import infrastructureAPI.backendGate.APIRoutineFunctions;
import infrastructureAPI.backendGate.APIScript;
import infrastructureAPI.backendGate.GeneralScript;
import infrastructureAPI.backendGate.SharedAPIParameters;

/**
 * Description: 
 *
 */
public class terminate_nodes extends GeneralScript {
	
	protected List<NodeDescriptor> targetNodes;
	protected int mandatoryParametersNumber;


	/**
	 * @param args
	 */
	public terminate_nodes(String[] args) {
		super(args);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		terminate_nodes terminator = new terminate_nodes(args);

	}

	@Override
	public void checkNotGeneralOptionalParameters() {
		
	}

	@Override
	public void composeMandatoryParameters() {
		int mandatoryParameterIndex = optionalParameterIndex;
		
		// retrieve the subcloudID
		arguments.contains(SharedAPIParameters.SUBCLOUD_ID_OPTION);
		
		
		// retrieve the targetNodes identities
		targetNodes = new Vector<NodeDescriptor>();
		for(int i=mandatoryParameterIndex; i<arguments.size(); i++){
			targetNodes.add( new ConcreteNodeDescriptor(arguments.get(i)));
		}
		System.out.println("targetNodes are:\n"+targetNodes);
				
	}

	@Override
	public APIScript instantiateAPIScript() {
		try {
			requester = generateLocalRequester();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return new TerminateNodes(requester, targetNodes);
	}

	@Override
	public boolean verifyMandatoryParameters() {
		boolean ret = true;
		TerminateNodes terminate = new TerminateNodes();
		mandatoryParametersNumber = APIRoutineFunctions.extractParametersNumber(arguments);
		ret = (mandatoryParametersNumber >= terminate.mandatoryParametersNumber());
		if (ret == false)
			System.out.println( terminate.showUsage());
		return ret;
	}

	@Override
	public void waitingIncomingResposes() {
		ResponseMessage response = waitingOneIncomingResponse();
		try {
			System.out.println("arrived response: " +
					response.getAPIResponse().getResponse()+"\n" +
							"from: "+response.getSender());
		} catch (ResponseException e) {
			e.printStackTrace();
		}
	}

}
